#!/usr/bin/env bash

set -ex

#
# Control soft START STOP RESTART
#

################################################################################

PGDATA=${PGDATA:-/pgdata}

LNMP_WSL_COMMON_SOFT="nginx php mysql redis"

################################################################################

print_help_info(){
  exec echo -e "
Usage:

lnmp-wsl {start | restart | stop} {SOFT_NAME | all | common}

lnmp-wsl status

Example:

lnmp-wsl start php nginx mysql redis

lnmp-wsl stop common
"
}

_sudo(){
  command -v sudo > /dev/null && echo "sudo" || true
}

function _start(){
    for soft in "$@"
    do
    echo -e "\033[31mStart $soft\033[0m...\n"
    case "$soft" in
      redis )
          if ! [ -f /run/redis.log ];then $(_sudo) touch /run/redis.log; fi
          $(_sudo) redis-server \
              --logfile /run/redis.log \
              --bind 127.0.0.1 \
              --save 900 1 \
              --save 300 10 \
              --save 60 10000 \
              --dir /tmp \
              --appendonly yes \
              --pidfile /run/redis.pid \
              --daemonize yes \
              --aof-use-rdb-preamble yes

          # 禁用 RDB
          # --save ""
      ;;

      mongodb )
         if ! [ -d /data/db ];then $(_sudo) mkdir -p /data/db ; fi

          $(_sudo) mongod \
              --fork --logpath=/run/mongodb.error.log \
              --pidfilepath /run/mongodb.pid
      ;;

      memcached )
          $(_sudo) memcached \
              -uroot \
              -d \
              -P /run/memcached.pid
      ;;

      php )
          $(_sudo) php-fpm \
              -D \
              -R \
              -g /run/php-fpm.pid
      ;;

      nginx )
          if ! [ -f /etc/nginx/fastcgi.conf ];then $(_sudo) cp $WSL_HOME/lnmp/config/etc/nginx/fastcgi.conf /etc/nginx/fastcgi.conf > /dev/null 2>&1 || true ; fi

          $(_sudo) nginx -t
          # $(_sudo) nginx -g "pid /run/nginx.pid;"
          $(_sudo) nginx
      ;;

      postgresql )

      $(_sudo) mkdir -p "$PGDATA" || true
      $(_sudo) chown -R "$(id -u)" "$PGDATA"
      $(_sudo) chmod 700 "$PGDATA"

      $(_sudo) mkdir -p /var/run/postgresql
      $(_sudo) chown -R "$(id -u)" /var/run/postgresql
      $(_sudo) chmod 777 /var/run/postgresql

      /usr/lib/postgresql/10/bin/pg_ctl -D "$PGDATA" \
                                        start
      ;;

      mysql )
         $(_sudo) mysqld_safe -D
      ;;

      * )
          $(_sudo) service $soft start
      ;;
esac
done
}

function _stop(){
  for soft in "$@"
  do
    echo -e "\033[31mStop $soft\033[0m...\n"
  case "$soft" in
    php )
        $(_sudo) kill $(cat /run/php-fpm.pid)
    ;;

    nginx )
        # $(_sudo) nginx -s stop -g "pid /run/nginx.pid;"

        $(_sudo) nginx -s stop
    ;;

    redis )
        redis-cli shutdown
    ;;

    memcached )
        $(_sudo) kill $(cat /run/memcached.pid)
    ;;

    mongodb )
        $(_sudo) kill $(cat /run/mongodb.pid)
    ;;

    postgresql )
        /usr/lib/postgresql/10/bin/pg_ctl -D "$PGDATA" stop
    ;;

    mysql )
       pid=`ps -aux | grep /usr/sbin/mysqld | awk -F " " '{print $2}' | head -1`
       $(_sudo) kill -9 $pid
    ;;

    * )
        $(_sudo) service $soft stop
    ;;
  esac
done
}

_restart(){
  for soft in "$@"
  do
  _stop $soft
  _start $soft
done
}

if [ -z "$1" ];then
    print_help_info
fi

if [ "$1" = stop ];then
  shift
  if [ "$1" = 'all' ];then
    set +e
    clear
    _stop nginx mysql php redis memcached mongodb ssh postgresql
    exit 0
  elif [ "$1" = 'common' ];then
    set +e
    clear
    for soft in $LNMP_WSL_COMMON_SOFT
      do
      _stop $soft
      done
    exit 0
  fi
  _stop "$@"
  exit 0
fi

if [ "$1" = start ];then
  shift
  if [ "$1" = 'all' ];then
    set +e
    clear
    _start nginx mysql php redis memcached mongodb ssh postgresql
    exit 0
  elif [ "$1" = 'common' ];then
    set +e
    clear
    for soft in $LNMP_WSL_COMMON_SOFT
      do
      _start $soft
      done
    exit 0
  fi
  _start "$@"
  exit 0
fi

if [ "$1" = restart ];then
  shift
  if [ "$1" = 'all' ];then
    set +e
    clear
    _restart nginx mysql php redis memcached mongodb ssh postgresql
    exit 0
  elif [ "$1" = 'common' ];then
    set +e
    clear
    for soft in $LNMP_WSL_COMMON_SOFT
      do
      _restart $soft
      done
    exit 0
  fi
  _restart "$@"
  exit 0
fi

if [ "$1" = 'status' ];then
    ps aux | grep nginx --color=auto
    echo "===>"
    ps aux | grep php-fpm --color=auto
    echo "===>"
    ps aux | grep mysql --color=auto
    echo "===>"
    ps aux | grep redis-server --color=auto
    echo "===>"
    ps aux | grep memcached --color=auto
    echo "===>"
    ps aux | grep mongod --color=auto
    echo "===>"
    ps aux | grep postgres --color=auto
    echo "===>"
    ps aux | grep sshd --color=auto
fi
